package com.yanchangapp.ui.activity.purchase.contract;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter.fragment.ContractListFgAdapter;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.ContractStatus;
import com.yanchangapp.model.bean.FilterBean;
import com.yanchangapp.model.bean.TabBean;
import com.yanchangapp.ui.activity.purchase.POrderFilterActivity;
import com.yanchangapp.ui.activity.purchase.POrderListActivity;
import com.yanchangapp.ui.fragment.purchase.ContractListFragment;
import com.yanchangapp.ui.fragment.purchase.PListFragment;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.widget.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

public class ContractListActivity extends BaseActivity {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.btnCreate)
    Button btnCreate;
    @BindView(R.id.linearBottom)
    LinearLayout linearBottom;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPagerFixed viewPager;

    private List<TabBean> mTabBeanList;
    private List<ContractListFragment> mListFragments;
    private int currentSelection = 0;

    public static String REFRESH_CONTRACT_LIST = "REFRESH_CONTRACT_LIST";
    private String keywords = null;
    private String startTime = null;
    private String endTime  = null;

    public static final String KEYWORDS = "keyword";
    public static final String START_TIME = "startTime";
    public static final String END_TIME = "endTime";

    // 合同筛选信息...
    public static final String NOTIFY_CONTRACT_FILTER_DATA = "notify_contract_filter_data";

    public static void show(Activity activity){
        Intent intent = new Intent(activity,ContractListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        // 初始化并加载tab数据
        mTabBeanList = new ArrayList<>();
        mTabBeanList.add(new TabBean(ContractStatus.ALL.getName(),ContractStatus.ALL.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.WAIT_SUBMIT.getName(),ContractStatus.WAIT_SUBMIT.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.WAIT_APPROVAL.getName(),ContractStatus.WAIT_APPROVAL.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.HAS_PASS.getName(),ContractStatus.HAS_PASS.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.FROM_ORDER.getName(),ContractStatus.FROM_ORDER.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.HAS_REJECT.getName(),ContractStatus.HAS_REJECT.getStatus()));
        mTabBeanList.add(new TabBean(ContractStatus.HAS_DESTROY.getName(),ContractStatus.HAS_DESTROY.getStatus()));
        // 初始化列表Fragment
        mListFragments = new ArrayList<>();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_contract_list;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvPublishNow.setVisibility(View.VISIBLE);
        tvPublishNow.setText("筛选");
        tvToolbarTitle.setText("采购合同列表");

        // 设置tabLayout+viewPager
        for (int i=0;i<mTabBeanList.size();i++){
            mListFragments.add(createPListFragment(mTabBeanList.get(i)));
        }

        ContractListFgAdapter contractListFgAdapter = new ContractListFgAdapter(getSupportFragmentManager(), mListFragments, mTabBeanList);
        viewPager.setAdapter(contractListFgAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(mListFragments.size());
        tabLayout.setupWithViewPager(viewPager);
    }

    private ContractListFragment createPListFragment(TabBean tabBean){
        ContractListFragment contractListFragment = new ContractListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ContractListFragment.TITLE,tabBean.getTitle());
        bundle.putString(ContractListFragment.VALUE,tabBean.getParams());
        contractListFragment.setArguments(bundle);
        return contractListFragment;
    }

    @Override
    protected void initData() {
        mRxManager.on(REFRESH_CONTRACT_LIST, (Consumer<Boolean>) aBoolean -> justRefresh());
    }

    public void hideCreateBtn(){
        btnCreate.setVisibility(View.GONE);
    }

    public void showCreateBtn(){
        btnCreate.setVisibility(View.VISIBLE);
    }

    /**
     * 模拟刷新
     */
    private void justRefresh() {
        ContractListFragment fragment = mListFragments.get(currentSelection);
        fragment.handleRefresh();
    }


    @Override
    protected void initListener() {
        // 点击返回按钮回退
        ivToolbarNavigation.setOnClickListener(v -> finish());

        // 监听页面切换
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 当前选中哪个Fragment....
                currentSelection = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tvPublishNow.setOnClickListener(v -> ContractFilterActivity.show(ContractListActivity.this,keywords,startTime,endTime));

        // 新建合同...
        btnCreate.setOnClickListener(v -> ContractCreateActivity.show(ContractListActivity.this));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == AppConstant.RECODE_CONTRACT_FILTER_ATY){
                keywords =  data.getStringExtra(KEYWORDS);
                startTime = data.getStringExtra(START_TIME);
                endTime = data.getStringExtra(END_TIME);
                // 通知数据改变
                FilterBean filterBean = new FilterBean(keywords,startTime,endTime,
                        mTabBeanList.get(currentSelection).getTitle());
                mRxManager.post(NOTIFY_CONTRACT_FILTER_DATA,filterBean);
            }
        }
    }
}
